package com.example.demo.util;


import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.net.URIBuilder;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class LoginAndToken {


   public static  CloseableHttpClient httpClient = HttpClients.createDefault();
   public  static String token="";

   static {
       //创建HttpPost对象，设置URL地址 https://www.runoob.com
       HttpPost httpPost = new HttpPost("http://ny.shuanghui.net:9181/login");
       // 设置请求头Content-Type为application/json
       httpPost.setHeader(new BasicHeader("Content-Type", "application/json"));
       // 准备JSON格式的请求体
       String json = "{\"pwd\":\"yzxxh202306\",\"userCode\":\"666\"}";
       // 将JSON字符串转换为StringEntity，并设置字符编码为UTF-8
       StringEntity formEntity = new StringEntity(json, ContentType.parse("UTF-8"));
       httpPost.setEntity(formEntity);

       //使用HttpClient发起请求，获取response
       CloseableHttpResponse response = null;
       try {
           response = httpClient.execute(httpPost);
       } catch (IOException e) {
           e.printStackTrace();
       }
       //token

       //解析响应
       if(response.getCode() == 200){
           String content = null;
           try {
               content = EntityUtils.toString(response.getEntity(), "utf-8");
           } catch (IOException e) {
               e.printStackTrace();
           } catch (ParseException e) {
               e.printStackTrace();
           }
           JSONObject jsonObject = new JSONObject(content);
           token = jsonObject.getString("token");

       }
   }

//    //登陆并且获取token
//    public  String loginAddToken() throws IOException, ParseException, URISyntaxException {
//        //创建HttpPost对象，设置URL地址 https://www.runoob.com
//        HttpPost httpPost = new HttpPost("http://ny.shuanghui.net:9181/login");
//        // 设置请求头Content-Type为application/json
//        httpPost.setHeader(new BasicHeader("Content-Type", "application/json"));
//        // 准备JSON格式的请求体
//        String json = "{\"pwd\":\"yzxxh202306\",\"userCode\":\"666\"}";
//        // 将JSON字符串转换为StringEntity，并设置字符编码为UTF-8
//        StringEntity formEntity = new StringEntity(json, ContentType.parse("UTF-8"));
//        httpPost.setEntity(formEntity);
//
//        //使用HttpClient发起请求，获取response
//        CloseableHttpResponse response = httpClient.execute(httpPost);
//        //token
//
//        //解析响应
//        if(response.getCode() == 200){
//            String content = EntityUtils.toString(response.getEntity(), "utf-8");
//            JSONObject jsonObject = new JSONObject(content);
//            token = jsonObject.getString("token");
////            System.out.println(content);
////            System.out.println(token);
//        }
//        return token;
//    }

    //获取到机构orgId
    public static Map getOrgId() throws URISyntaxException, IOException, ParseException {
        URIBuilder uriBuilder = new URIBuilder("http://ny.shuanghui.net:9181/company/org/list?pageSize=20&pageNum=1");
        //设置参数
        uriBuilder.setParameter("pageSize","200");
        uriBuilder.setParameter("pageNum","1");

        HttpGet httpGet = new HttpGet(uriBuilder.build());

        httpGet.setHeader("Token",token);
        CloseableHttpResponse response1 = httpClient.execute(httpGet);
        String s ="";
        HashMap orgId = new HashMap<>();

        com.alibaba.fastjson.JSONObject data = GetData.getData(response1);
        String list = data.getString("list");
        JSONArray jsonArray = new JSONArray(list);
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject list1 = jsonArray.getJSONObject(i);

            orgId.put(list1.get("orgName"),list1.get("orgId"));
        }
        s = jsonArray.toString();
        return orgId;
    }

    //获取到漯河商品鸡orgId
    public Map luoHeOrgId() throws URISyntaxException, IOException, ParseException {
        Map orgId = getOrgId();
        HashMap<Object, Object> luoHeOrgMap = new HashMap<>();
        Iterator<Map.Entry<String, Object>> iterator = orgId.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String key = entry.getKey();
            if (key.contains("漯河禽业商品鸡")){
                luoHeOrgMap.put(key,orgId.get(key));
            }
        }
        Iterator<Map.Entry<Object, Object>> iterator1 = luoHeOrgMap.entrySet().iterator();
        while (iterator1.hasNext()){
            Map.Entry<Object, Object> next = iterator1.next();
            System.out.println("orgName: " + next.getKey() + ", orgId: " + next.getValue());
        }
        return luoHeOrgMap;

    }

    //根据条件获取到orgId
    public static Map getOrgIdByName(String name) throws URISyntaxException, IOException, ParseException {

        Map orgId = getOrgId();
        HashMap<Object, Object> orgIdName = new HashMap<>();
        Iterator<Map.Entry<String, Object>> iterator = orgId.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String key = entry.getKey();
            if (key.contains(name)){
                orgIdName.put(key,orgId.get(key));
            }
        }
        return orgIdName;
    }





}
